#include <bits/stdc++.h>
// #define int long long
#define mid ((l+r)>>1)
using namespace std;
int n,m,K;
int oa[400005],c[400005],a[400005];
char cc[20][400005];
bool d[20][400005];
long long ans[400005];
int mins[400005],tp[400005],lpos[400005],rpos[400005];
long long siz[400005];
int tpos[400005];
int winn[400005],winv[400005];
int dep[400005];
int tr[20];
void debug(int i,int l,int r){
	cout<<i<<" "<<l<<" "<<r<<" "<<winn[i]<<" "<<winv[i]<<" "<<siz[i]<<"\n";
	if(l==r) return ;
	debug(i*2,l,mid);
	debug(i*2+1,mid+1,r);
}
int now,ni;
long long tans;
void addp(){
	int ndep=1;
	int pos=tpos[now],val=a[now],onow=now;
	while(pos!=1){
		// cout<<pos<<" ";
		int dir=pos-((pos>>1)<<1);
		if(tp[pos>>1]==dir){
			if(dir==0){
				if(val>=ndep){
					ndep++;
					pos>>=1;
					tans-=siz[(pos<<1)|(dir^1)];
					winn[pos]=onow;
					winv[pos]=val;
					siz[pos]=onow;
					now=rpos[(pos<<1)|(dir^1)];
					continue;
				}
				else{
					tans-=onow;
					siz[pos]=0;
					winn[pos]=0;
					winv[pos]=0;
					break;
				}
			}
			else{
				if(val>=ndep){
					ndep++;
					pos>>=1;
					tans-=siz[(pos<<1)|(dir^1)];
					winn[pos]=onow;
					winv[pos]=val;
					siz[pos]=onow;
					continue;
				}
				else{
					tans-=onow;
					if(!siz[((pos>>1)<<1)|(dir^1)]){
						siz[((pos>>1)<<1)|(dir^1)]=winn[((pos>>1)<<1)|(dir^1)];
						tans+=winn[((pos>>1)<<1)|(dir^1)];
					}
					onow=winn[((pos>>1)<<1)|(dir^1)];
					val=winv[((pos>>1)<<1)|(dir^1)];
					ndep++;
					pos>>=1;
					winn[pos]=onow;
					winv[pos]=val;
					siz[pos]=onow;
					continue;
				}
			}
		}
		else{
			if(dir==0){
				if(mins[pos>>1]>val){
					// cout<<"qqq";
					tans-=onow;
					siz[pos]=0;
					winn[pos]=onow;
					winv[pos]=val;
					break;
				}
				else{
					siz[pos]=onow;
					winn[pos]=onow;
					winv[pos]=val;
					break;
				}
			}
			else{
				if(siz[((pos>>1)<<1)|(dir^1)]) assert(0);
				ndep++;
				pos>>=1;
				tans-=siz[(pos<<1)|(dir^1)];
				winn[pos]=onow;
				winv[pos]=val;
				siz[pos]=onow;
			}
		}
	}
	// cout<<"\n";
}
void solve(){
	for(int i=1;i<=n;i++) ans[i]=0;//cout<<a[i]<<" "; cout<<"\n";
	ans[1]=1;
	for(int i=1;i<=K;i++){
		mins[1]=0;
		ni=i;
		for(int j=1;j<=i;j++) tr[j]=0;
		//solve (1<<(i-1))+1 (1<<i)
		dep[1]=i;
		lpos[1]=1,rpos[1]=(1<<i);
		siz[1]=1ll*(lpos[1]+rpos[1])*(rpos[1]-lpos[1]+1)/2;
		winn[1]=winv[1]=0;
		tr[dep[1]]++;
		tp[1]=d[dep[1]][tr[dep[1]]];
		if(tp[1]==0) mins[2]=max(dep[1],mins[1]),mins[3]=mins[1];
		else mins[3]=max(dep[1],mins[1]),mins[2]=mins[1];
		// cout<<"OK\n";
		for(int j=2;j<(1<<(i+1));j++){
			dep[j]=dep[j>>1]-1;
			if(j&1) lpos[j]=((lpos[j>>1]+rpos[j>>1])>>1)+1,rpos[j]=rpos[j>>1];
			else lpos[j]=lpos[j>>1],rpos[j]=((lpos[j>>1]+rpos[j>>1])>>1);
			siz[j]=1ll*(lpos[j]+rpos[j])*(rpos[j]-lpos[j]+1)/2;
			winn[j]=winv[j]=0;
			if(lpos[j]==rpos[j]){
				tpos[lpos[j]]=j;
				continue;
			}
			tr[dep[j]]++;
			tp[j]=d[dep[j]][tr[dep[j]]];
			if(tp[j]==0) mins[(j<<1)]=max(dep[j],mins[j]),mins[(j<<1)|1]=mins[j];
			else mins[(j<<1)|1]=max(dep[j],mins[j]),mins[(j<<1)]=mins[j];
		}
		// pdfs(1,1,(1<<i),i);
		tans=siz[1]; //cout<<tans<<"\n";
		for(int j=1;j<=(1<<(i-1));j++){
			now=j;
			addp();
			j=now;
			//cout<<tans<<"\n";
		}
		// debug(1,1,(1<<i));
		if(now==(1<<i)){
			for(int l=(1<<(i-1))+1;l<=now;l++) ans[l]=tans;
			continue;
		}
		// assert(now<=(1<<(i-1)));
		// cout<<tans<<"\n";
		for(int j=(1<<(i-1))+1;j<=min((1<<i),n);j++){
			now=j;
			addp();
			// cout<<j<<" "<<now<<"\n";
			for(int l=j;l<=now;l++) ans[l]=tans;
			j=now;
		}
	}
	// for(int i=1;i<=n;i++) if(ans[i]<0) cout<<i<<" "; cout<<"\n";
	// for(int i=1;i<=n;i++) cout<<ans[i]<<" "; cout<<"\n";
	long long ret=0;
	for(int i=1;i<=m;i++) ret^=(1ll*i*ans[c[i]]);
	cout<<ret<<"\n";
}
signed main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>oa[i];
	for(int i=1;i<=m;i++) cin>>c[i];
	int tmp=1;
	while(tmp<n) tmp*=2,K++;
	for(int i=1;i<=K;i++){
		for(int j=1;j<=(1<<(K-i));j++){
			cin>>cc[i][j];
			d[i][j]=cc[i][j]-'0';
		}
	}
	int t; cin>>t;
	while(t--){
		int x[4]; cin>>x[0]>>x[1]>>x[2]>>x[3];
		for(int i=1;i<=n;i++) a[i]=oa[i]^x[i%4];
		solve();
		// cerr<<t<<"\n";
	}
	return 0;
}
/*
Notices
1. Multitest: print endl before return
2. Multitest: clear (test random ordered sample)
3. Memory
4. long long / int range
5. corner case (minimum and maximum)
6. non-void return value
7. beizeng shangjie
8. -Wall -Wextra -fsanitize=undefined when testing samples, -O2 when testing speed (do not write O2 in the code)
*/